{extend name='view/common/_container'}
{block name="content"}
<div class="ibox float-e-margins">
<input type="hidden" name='app_id' id='app_id' value="{$info.app_id}" />
	<div class="ibox-content layui-form">
		<div class="form-horizontal" id="CodeInfoForm">
			<div class="row">
				<div class="col-sm-12">
				<!-- form start -->
					<div class="form-group">
						<label class="col-sm-2 control-label">应用名：</label>
						<div class="col-sm-9">
							<input type="text" id="name" value="{$info.name}" name="name" class="form-control" placeholder="请输入应用名称">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">应用目录名：</label>
						<div class="col-sm-9">
							<input type="text" id="app_dir" value="{$info.app_dir}" name="app_dir" class="form-control" placeholder="请输入生成的应用目录">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">生成状态：</label>
						<div class="col-sm-9">
							<?php if(!isset($info['status'])){ $info['status'] = 1; }; ?>
							<input name="status" value="1" type="radio" {if condition="$info.status eq '1'"}checked{/if} title="启用">
							<input name="status" value="0" type="radio" {if condition="$info.status eq '0'"}checked{/if} title="禁用">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">应用类型：</label>
						<div class="col-sm-9">
							<?php if(!isset($info['app_type'])){ $info['app_type'] = 1; }; ?>
							<input name="app_type" value="1" lay-filter="hope" type="radio" {if condition="$info.app_type eq '1'"}checked{/if} title="后台应用">
							<input name="app_type" value="2" lay-filter="hope" type="radio" {if condition="$info.app_type eq '2'"}checked{/if} title="api应用">
							<input name="app_type" value="3" lay-filter="hope" type="radio" {if condition="$info.app_type eq '3'"}checked{/if} title="cms应用">
						</div>
					</div>
					<div class="form-group" id="loginTable">
						<label class="col-sm-2 control-label">登录数据表：</label>
						<div class="col-sm-9">
							<input type="text" id="login_table" value="{$info.login_table}" name="login_table" class="form-control" placeholder="请输入登录数据表">
						</div>
					</div>
					<div class="form-group" id="loginFields">
						<label class="col-sm-2 control-label">登录字段：</label>
						<div class="col-sm-9">
							<input type="text" id="login_fields" value="{$info.login_fields}" name="login_fields" class="form-control" placeholder="用户名|密码，例如 username|password">
						</div>
					</div>
					<div class="form-group" id="pkId">
						<label class="col-sm-2 control-label">登录表主键：</label>
						<div class="col-sm-9">
							<input type="text" id="pk" value="{$info.pk}" name="pk" class="form-control" placeholder="登录数据表主键ID">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">访问域名：</label>
						<div class="col-sm-9">
							<input type="text" id="domain" value="{$info.domain}" name="domain" class="form-control" placeholder="请输入访问域名">
							<span class="help-block m-b-none">应用绑定访问域名还需在config/app.php 绑定</span>
						</div>
					</div>
					
				<!-- form end -->
				</div>
			</div>
			<div class="hr-line-dashed"></div>
			<div class="row btn-group-m-t">
				<div class="col-sm-9 col-sm-offset-1">
					<button type="button" class="btn btn-primary" onclick="{if condition="$info.app_id neq ''"}CodeInfoDlg.update(){else/}CodeInfoDlg.add(){/if}" id="ensure">
						<i class="fa fa-check"></i>&nbsp;确认提交
					</button>
					<button type="button" class="btn btn-danger" onclick="CodeInfoDlg.close()" id="cancel">
						<i class="fa fa-eraser"></i>&nbsp;取消
					</button>
				</div>
			</div>
		</div>
	</div>
</div>
<script src="__PUBLIC__/static/js/plugins/layui/layui.js?t=1498856285724" charset="utf-8"></script>
<script>
layui.config({dir: '__PUBLIC__/static/js/plugins/layui/'});
	layui.use(['layer', 'form'], function () {
	window.layer = layui.layer;
	window.form = layui.form;
	
	form.on('radio(hope)',function(data){
		if(data.value == 1){
			$("#loginTable").show();
			$("#loginFields").show();
			$("#group_status").show();
			$("#pkId").show();
		}else{
			$("#loginTable").hide();
			$("#loginFields").hide();
			$("#group_status").hide();
			$("#pkId").hide();
		}
	
	});	
	
});

$(function(){
	var type = $("input[name='app_type']:checked").val();
	if(type == 2 || type == 3){
		$("#loginTable").hide();
		$("#loginFields").hide();
		$("#pkId").hide();
	}
});

var CodeInfoDlg = {
	CodeInfoData: {},
	deptZtree: null,
	pNameZtree: null,
	validateFields: {
		name: {
			validators: {
				notEmpty: {
					message: '应用名称不能为空'
	 			}
	 		}
	 	},
		app_dir: {
			validators: {
				notEmpty: {
					message: '应用目录不能为空'
	 			},
				regexp: {
					regexp: /^[a-z]+$/,
					message: '小写英文字母'
	 			}
	 		}
	 	},
	 }
}


CodeInfoDlg.clearData = function () {
	 this.CodeInfoData = {};
};


CodeInfoDlg.set = function (key, val) {
	 this.CodeInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
	 return this;
};


CodeInfoDlg.get = function (key) {
	 return $("#" + key).val();
};


CodeInfoDlg.close = function () {
	 var index = parent.layer.getFrameIndex(window.name);
	 parent.layer.close(index);
};


CodeInfoDlg.collectData = function () {
	this.set('app_id').set('name').set('app_dir').set('login_table').set('login_fields').set('domain').set('pk');
};


CodeInfoDlg.add = function () {
	 this.clearData();
	 this.collectData();
	 if (!this.validate()) {
	 	return;
	 }
	 var status = $("input[name = 'status']:checked").val();
	 var app_type = $("input[name = 'app_type']:checked").val();
	 var group_status = $("input[name = 'group_status']:checked").val();
	 var tip = '添加';
	 var ajax = new $ax(Feng.ctxPath + "/Sys.Application/add", function (data) {
	 	if ('00' === data.status) {
	 		Feng.success(tip + "成功" );
	 		window.parent.CodeGoods.table.refresh();
	 		CodeInfoDlg.close();
	 	} else {
	 		Feng.error(tip + "失败！" + data.msg + "！");
		 }
	 }, function (data) {
	 	Feng.error("操作失败!" + data.msg + "!");
	 });
	 ajax.set('status',status);
	 ajax.set('app_type',app_type);
	 ajax.set('group_status',group_status);
	 ajax.set(this.CodeInfoData);
	 ajax.start();
};


CodeInfoDlg.update = function () {
	 this.clearData();
	 this.collectData();
	 if (!this.validate()) {
	 	return;
	 }
	 var status = $("input[name = 'status']:checked").val();
	 var app_type = $("input[name = 'app_type']:checked").val();
	 var group_status = $("input[name = 'group_status']:checked").val();
	 var tip = '修改';
	 var ajax = new $ax(Feng.ctxPath + "/Sys.Application/update", function (data) {
	 	if ('00' === data.status) {
	 		Feng.success(tip + "成功" );
	 		window.parent.CodeGoods.table.refresh();
	 		CodeInfoDlg.close();
	 	} else {
	 		Feng.error(tip + "失败！" + data.msg + "！");
		 }
	 }, function (data) {
	 	Feng.error("操作失败!" + data.msg + "!");
	 });
	 ajax.set('status',status);
	 ajax.set('app_type',app_type);
	 ajax.set('group_status',group_status);
	 ajax.set(this.CodeInfoData);
	 ajax.start();
};


CodeInfoDlg.validate = function () {
	  $('#CodeInfoForm').data("bootstrapValidator").resetForm();
	  $('#CodeInfoForm').bootstrapValidator('validate');
	  return $("#CodeInfoForm").data('bootstrapValidator').isValid();
};


$(function () {
	   Feng.initValidator("CodeInfoForm", CodeInfoDlg.validateFields);
});
</script>


{/block}
